﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Business;
using DataAccess;
using UnitHelper;

namespace BaseWeb.JQuery.方法汇聚
{
    /// <summary>
    /// _3 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class _3 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string GetMenuByParentKeyId(string ParentKeyId)
        {
            List<li_Menu> arrayMenu = CommonSystem.Instance.Service.GetListByWhere<li_Menu>(c => c.ParentKeyId == ParentKeyId).OrderBy(c => c.OrderIndex).ToList();
            return JsonHelper.ToJson<li_Menu>(arrayMenu, "KeyId", "Name");
        }

        [WebMethod]
        public string GetParentKeyId(string KeyId)
        {
            li_Menu enMenu = CommonSystem.Instance.Service.GetInfoByKeyId<li_Menu>(KeyId);
            if (enMenu == null)
                return "0";
            else
                return enMenu.ParentKeyId;
        }

        [WebMethod]
        public void ChangeOrderIndex(string SourceKeyId, string PreObjKeyId, string AfterObjKeyId)
        {
            li_Menu enSource = null;
            li_Menu enPreObj = null;
            li_Menu enAfterObj = null;
            if (!string.IsNullOrEmpty(SourceKeyId))
            {
                enSource = CommonSystem.Instance.Service.GetInfoByKeyId<li_Menu>(SourceKeyId.Replace("li_", ""));
            }
            if (!string.IsNullOrEmpty(PreObjKeyId))
            {
                enPreObj = CommonSystem.Instance.Service.GetInfoByKeyId<li_Menu>(PreObjKeyId.Replace("li_", ""));
            }
            if (!string.IsNullOrEmpty(AfterObjKeyId))
            {
                enAfterObj = CommonSystem.Instance.Service.GetInfoByKeyId<li_Menu>(AfterObjKeyId.Replace("li_", ""));
            }
            if (enSource != null)
            {
                if (enPreObj == null && enAfterObj != null)
                {
                    enSource.OrderIndex = (Convert.ToDouble(enAfterObj.OrderIndex) * 2 - 1) / 2;
                }
                else if (enPreObj != null && enAfterObj == null)
                {
                    enSource.OrderIndex = (Convert.ToDouble(enPreObj.OrderIndex) * 2 + 1) / 2;
                }
                else
                {
                    enSource.OrderIndex = (Convert.ToDouble(enPreObj.OrderIndex) + Convert.ToDouble(enAfterObj.OrderIndex)) / 2;
                }
                CommonSystem.Instance.Service.UpdateEntity<li_Menu>(enSource);
            }

        }




    }
}
